package com.ruoyi.farmer.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.farmer.domain.TraceUserCrops;
import com.ruoyi.farmer.domain.vo.CropsImageVo;
import com.ruoyi.farmer.service.TraceUserCropsService;
import com.ruoyi.farmer.utils.Base64Util;
import com.ruoyi.system.api.RemoteLogisticsService;
import com.ruoyi.system.api.domain.TraceTransport;
import com.ruoyi.system.api.domain.vo.CropsVo;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

/**
 * 农户
 * @author 王泽超的电脑
 */
@RestController
@RequestMapping("/farmer")
public class FarmerController extends BaseController {

    @Value("${fdfs.address}")
    private String address;

    @Autowired
    private TraceUserCropsService traceUserCropsService;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;


    /**
     *  修改作物状态 并添加运输
     *
     * @param traceTransport
     * @return
     */
    @PostMapping("/addTransport")
    public AjaxResult addTransport(@RequestBody TraceTransport traceTransport){
        int i = traceUserCropsService.addTransport(traceTransport);
        return i>0?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 获取部门下的司机
     *
     * @param deptId
     * @return
     */
    @GetMapping("/getAllDriverByDeptId/{deptId}")
    public AjaxResult getAllDriverByDeptId(@PathVariable("deptId") String deptId){
        List<SysUser> list = traceUserCropsService.getAllDriverByDeptId(Integer.valueOf(deptId));
        return AjaxResult.success(list);
    }

    /**
     * 获取部门下的工厂
     * @param deptId
     * @return
     */
    @GetMapping("/getFactoryByDeptId/{deptId}")
    public AjaxResult getFactoryByDeptId(@PathVariable("deptId") Integer deptId){
        return AjaxResult.success(traceUserCropsService.getFactoryByDeptId(deptId));
    }

    /**
     * 上传农作物图片
     * @param cropsImageVo
     * @return
     * @throws IOException
     */
    @PostMapping("imageUpload")
    public AjaxResult imageUpload(@RequestBody CropsImageVo cropsImageVo) throws IOException {
        String imageBase64 = cropsImageVo.getImageBase64().replace("data:image/jpeg;base64,", "");
        File file = Base64Util.base64ConvertFile(imageBase64);
        StorePath storePath = this.fastFileStorageClient.uploadImageAndCrtThumbImage(Files.newInputStream(file.toPath()),file.length(),"jpg",null);
        return AjaxResult.success(address+storePath.getFullPath());
    }

    /**
     * 添加作物
     *
     * @param traceCrops
     * @return
     */
    @PostMapping("/saveCrops")
    @RepeatSubmit
    public AjaxResult saveCrops(@RequestBody TraceUserCrops traceCrops) {
        traceCrops.setUsername(SecurityUtils.getUsername());
        return AjaxResult.success(traceUserCropsService.saveCrops(traceCrops));
    }

    /**
     * 根据用户名获取农产品
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author steakliu
     * @Date 23:12 2020/10/27 0027
     * @Param [username]
     **/
    @GetMapping("/getCropsByUsername")
    public AjaxResult getCropsByUsername(String username) {
        startPage();
        List<TraceUserCrops> cropsList = traceUserCropsService.getCropsByUsername(username);
        return AjaxResult.success(getDataTable(cropsList));
    }

    /**
     * 修改农产品加工状态
     *
     * @param cropsVo
     * @return
     */
    @InnerAuth
    @PostMapping("/updateMachingStatus")
    public R<Integer> updateMachingStatus(@RequestBody CropsVo cropsVo){
        int i = traceUserCropsService.updateMachingStatus(cropsVo);
        return R.ok(i);
    }

    /**
     *修改农产品填写状态
     *
     * @param cropsId
     * @return
     */
    @InnerAuth
    @GetMapping("/updateProductWriteStatus/{cropsId}")
    public R<Integer> updateProductWriteStatus(@PathVariable("cropsId") String cropsId){
        int i=traceUserCropsService.updateProductWriteStatus(cropsId);
        return R.ok(i);
    }

    /**
     * 农产品出库
     *
     * @param cropsId
     * @return
     */
    @InnerAuth
    @GetMapping("/productOutFactory/{cropsId}")
    public R<Integer> productOutFactory(@PathVariable("cropsId") String cropsId){
        int i=traceUserCropsService.productOutFactory(cropsId);
        return R.ok(i);
    }
}
